<template>
  <div class="login_container">
    <div class="content">
      <div class="avatar_box">
        <img src="../assets/jr.png" alt="">
      </div>
      <!-- <el-input v-model="user" placeholder="请输入内容"></el-input><br><br>
      <el-input v-model="pass" placeholder="请输入密码" show-password></el-input><br><br>
      <el-button type="primary" round @click="register">注册</el-button>
      <el-button type="success" round  @click="login">登录</el-button> -->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="formData">
        <el-form-item prop="username">
        <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
      </el-form-item>
        <el-form-item prop="password">
        <el-input v-model="loginForm.password" prefix-icon="el-icon-s-tools" type="password"></el-input>
      </el-form-item>
      <el-form-item class="forms">
        <el-button type="primary" @click="login">登录</el-button>
         <el-button type="success" @click="resetLoginForm">重置</el-button>
      </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      //登陆表单的数据绑定对象
      loginForm:{
        username:'admin',
        password:'123456'
      },
      //表单的验证规则
      loginFormRules:{
        username:[
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max:10,message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
        password:[
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max:15,message: '长度在 6 到 15 个字符', trigger: 'blur' },
        ]
      }
    }
    
  },
  methods:{
    //点击重置按钮、重置登录表单
    resetLoginForm(){
      console.log(this);
      this.$refs.loginFormRef.resetFields();
    },
    login(){
      this.$refs.loginFormRef.validate(async valid=>{
        if(!valid) return;
        //data 重命名res
        const {data: res} = await this.$http.post('login',this.loginForm);
        if(res.meta.status !== 200) return this.$message.error('登录失败！');
        this.$message.success('登录成功！');
        // console.log(res.data.token)
        window.sessionStorage.setItem('token',res.data.token)//接受一个键名和值作为参数，将会把键名添加到存储中
        this.$router.push('/home');
      });
    }
  },
  created(){
  
}
}
</script>

<style scoped>
.login_container{
  height: 100%;
  display: flex;
  justify-content: center;
  background-color: wheat;
}
.content{
  width: 350px;
  height: 300px;
  text-align: center;
  border-radius: 3px;
  margin-top: 200px;
  /* transform: translate(0,-50%s); */
}
.avatar_box{
  height: 40px;
  width: 80px;
}
.avatar_box img{
  border-radius: 50%;
  margin-left: -100px;
}
.forms{
  display: flex;
  justify-content: center;

}
.formData{
  margin-top: 80px;
}
</style>
